package com.geeker.module.sys.dict.controller;

import com.geeker.common.annotation.Log;
import com.geeker.common.annotation.PreAuth;
import com.geeker.common.annotation.RepeatSubmit;
import com.geeker.common.api.CommonResult;
import com.geeker.common.enums.BusinessType;
import com.geeker.common.util.PageUtil;
import com.geeker.module.sys.dict.domain.SysDictType;
import com.geeker.module.sys.dict.dto.SysDictTypeDto;
import com.geeker.module.sys.dict.sevice.SysDictTypeService;
import com.mybatisflex.core.paginate.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Tag(name = "数据字典类型")
@RequestMapping("/sys/dict/type")
public class SysDictTypeController {

    @Autowired
    private SysDictTypeService sysDictTypeService;

    @Operation(summary = "分页")
    @PostMapping("page")
    public CommonResult<Page<SysDictType>> page(@RequestBody SysDictTypeDto dto) {
        Page<SysDictType> page = PageUtil.getPage(dto);
        Page<SysDictType> iPage = sysDictTypeService.page(page);
        return CommonResult.success(iPage);
    }

    @Operation(summary = "详情")
    @PostMapping(value = "detail/{dictId}")
    public CommonResult<SysDictType> detail(@PathVariable Long dictId) {
        return CommonResult.success(sysDictTypeService.getById(dictId));
    }

    @Operation(summary = "新增")
    @PostMapping("add")
    @RepeatSubmit
    public CommonResult add(@Validated @RequestBody SysDictType dict) {
        return CommonResult.success(sysDictTypeService.add(dict));
    }

    @Operation(summary = "编辑")
    @PostMapping("edit")
    public CommonResult edit(@Validated @RequestBody SysDictType dict) {
        return CommonResult.success(sysDictTypeService.edit(dict));
    }

    @Operation(summary = "删除")
    @Log(title = "字典类型", businessType = BusinessType.DELETE)
    @PostMapping("/delete/{dictIds}")
    public CommonResult remove(@PathVariable List<Long> dictIds) {
        sysDictTypeService.delete(dictIds);
        return CommonResult.success();
    }

    @Operation(summary = "刷新字典缓存")
    @Log(title = "字典类型", businessType = BusinessType.CLEAN)
    @PostMapping("/refreshCache")
    public CommonResult refreshCache() {
        sysDictTypeService.resetDictCache();
        return CommonResult.success();
    }

    @Operation(summary = "导出")
    @Log(title = "字典类型", businessType = BusinessType.EXPORT)
    @PreAuth("sys:dict:type:export")
    @PostMapping("/export")
    public void export(@RequestBody SysDictTypeDto dto) {
        sysDictTypeService.export(dto);
    }
}
